package siau.android.base

import android.content.Context
import android.util.TypedValue
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import hhm.android.library.utils.OnMultiClickListener
import siau.android.http.model.ChildModel
import siau.android.http.model.ShoeSizeListModel
import siau.android.library.KeyString

class BottomShoeSizeClassSelectSheet(context: Context, var list: ArrayList<ShoeSizeListModel>, var listener: GetShoeSizeTypeListener) : BottomSheetDialog(context) {
    lateinit var adapter: BottomShoeSizeClassSelectAdapter
    lateinit var select: ShoeSizeListModel

    init {
        list.forEach {
            if (it.currentType == 1) {
                select = it
            }
            it.select = it.currentType == 1
        }
        createView()
    }

    private fun createView() {
        val bottomSheetView: View = layoutInflater.inflate(R.layout.item_bottom_select_baby, null)
        setContentView(bottomSheetView)

        val tv: TextView = bottomSheetView.findViewById(R.id.item_bottom_select_baby_top_tv)
        val recycler: RecyclerView = bottomSheetView.findViewById(R.id.item_bottom_select_baby_recycler)
        val sure: TextView = bottomSheetView.findViewById(R.id.item_bottom_select_baby_sure)
        val cancel: TextView = bottomSheetView.findViewById(R.id.item_bottom_select_baby_cancel)
        val addBaby: TextView = bottomSheetView.findViewById(R.id.item_bottom_select_baby_add)
        addBaby.visibility = View.GONE

        tv.text = context.getString(R.string.please_select_shoe_size_class)
        recycler.layoutManager = LinearLayoutManager(context)

        adapter = BottomShoeSizeClassSelectAdapter(context, list, object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                val pos = v?.tag as Int
                list.forEach {
                    it.select = false
                }
                list[pos].select = true
                select = list[pos]
                adapter.notifyDataSetChanged()
            }
        })
        recycler.adapter = adapter

        cancel.setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                dismiss()
            }
        })
        sure.setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                listener.getShoeSizeType(select)
                dismiss()
            }
        })

        // 注意：这里要给layout的parent设置peekHeight，而不是在layout里给layout本身设置，下面设置背景色同理，坑爹！！！
        val bottomSheetBehavior: BottomSheetBehavior<*> = BottomSheetBehavior.from(
            bottomSheetView.parent as View
        )
        bottomSheetView.measure(0, 0)
        behavior.peekHeight = bottomSheetView.measuredHeight

        bottomSheetBehavior.peekHeight = bottomSheetView.measuredHeight
        //允许点击外部关闭，不允许滑动关闭
        bottomSheetBehavior.isHideable = false
        (bottomSheetView.parent as View).setBackgroundColor(context.resources.getColor(R.color.trans))
    }

    fun dp2px(dpValue: Float?): Int {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, dpValue!!, context.resources.displayMetrics
        ).toInt()
    }
}